package com.auerdi.ui.detail.cutprice;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.detail.cutprice.child.ADCutChildLieft;
import com.auerdi.ui.detail.cutprice.child.ADCutChildRight;
import com.auerdi.ui.detail.cutprice.event.POPtomyCut;
import com.example.eggframe.GoHomeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADCutHomeFragMent extends SwipeBackFragment {

    Unbinder unbinder;
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.aaa)
    LinearLayout aaa;
    @BindView(R.id.bbb)
    LinearLayout bbb;
    @BindView(R.id.ccc)
    LinearLayout ccc;
    @BindView(R.id.imgaaa)
    ImageView imgaaa;
    @BindView(R.id.imgbbb)
    ImageView imgbbb;
    @BindView(R.id.imgccc)
    ImageView imgccc;
    @BindView(R.id.txt_bbb)
    TextView txtBbb;
    @BindView(R.id.txt_ccc)
    TextView txtCcc;

    public static ADCutHomeFragMent newInstance() {

        Bundle args = new Bundle();

        ADCutHomeFragMent fragment = new ADCutHomeFragMent();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_cut_home, container, false);


        unbinder = ButterKnife.bind(this, view);
        View tpview = attachToSwipeBack(view);


        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        aaa.setOnClickListener(v -> EventBusActivityScope.getDefault(getActivity()).post(new GoHomeEvent()));
        bbb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);

            }
        });
        ccc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });


        return tpview;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {


            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return ADCutChildLieft.newInstance();
                } else {
                    return ADCutChildRight.newInstance();
                }
            }

            @Override
            public int getCount() {
                return 2;
            }


        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0) {


                    txtBbb.setTextColor(Color.parseColor("#ff2741"));
                    txtCcc.setTextColor(Color.parseColor("#000000"));
                    imgbbb.setImageResource(R.mipmap.cu_a);
                    imgccc.setImageResource(R.mipmap.me_cut_a);


                }
                if (position == 1) {
                    txtBbb.setTextColor(Color.parseColor("#000000"));
                    txtCcc.setTextColor(Color.parseColor("#ff2741"));
                    imgbbb.setImageResource(R.mipmap.cu);
                    imgccc.setImageResource(R.mipmap.mecut);


                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        viewPager.setAdapter(new WechatPagerFragmentAdapter(getChildFragmentManager()
//                , "关注", "发现"));
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(POPtomyCut event) {
        viewPager.setCurrentItem(1);


        popTo(ADCutHomeFragMent.class, false);
//        popChild();

    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }


}
